<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

<script>
    let raceArr = [
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("3s");
                resolve('race-----3s');
            }, 3000);
        }),
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("2s");
                resolve('race-----2s');
            }, 2000);
        }),
        new Promise((resolve, reject) => {
            setTimeout(_ => {
                console.log("5s");
                resolve('race-----5s');
            }, 5000);
        }),
    ];

    // function promiseAll(promiseArr) {
    //     const results = [];
    //     let resCnt = 0;
    //     return new Promise((resolve, reject) => {
    //         // 没有任务
    //         if (promiseArr.length === 0) {
    //             return resolve(results);
    //         }
    //         promiseArr.forEach((promise, index) => {
    //             promise.then(
    //                 res => {
    //                     resCnt++;
    //                     results[index] = res;
    //                     if (resCnt === promiseArr.length) {
    //                         resolve(results);
    //                     }
    //                 }, err => {
    //                     reject(err);
    //                 })
    //         });
    //     });
    // }
    promiseAll(raceArr);

    function promiseAll(arr) {
        let result = [];
        return new Promise((resolve, reject) => {
            for (let i = 0; i < arr.length; i++) {
                arr[i].then(res => {
                    result[i] = res;
                    if (result.length === arr.length) {
                        resovle(result);
                    }
                }, err => {
                    reject();
                })
            }
        })
    }

    function promiseAll(arr) {
        let result = [];
        return new Promise((resolve, reject) => {
            for (let i = 0; i < arr.length; i++) {
                arr[i].then(res => {
                    result[i] = res;
                    if (result.length === arr.length) {
                        resolve(result);
                    }
                }, err => {
                    reject(err);
                })
            }
        })
    }

    function promiseAll(arr) {
        let result = [];
        return new Promise((resolve, reject) => {
            for (let i = 0; i < arr.length; i++) {
                arr[i].then(res => {
                    result[i] = res;
                    if (result.length === arr.length)
                        resolve(result);
                }, err => {
                    reject(err)
                })
            }
        })
    }

</script>

</html>